<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\View;
class Register extends Index
{
    public function __construct(Request $request){
        parent::__construct($request);
    }
    /**
    * 跳转到首页
    */
    public function index(Request $request)
    {  
        $this->assign('title','注册');
        return view('index');
    }
    /**
    * 注册
    */ 
    public function reg(Request $request)
    {
        //是否为ajax提交
        if(request()->isAjax()){
            //用户是否存在
            $regs=Db::name('user')->where(['username'=>$request->post('username')])->find();
            if($regs){
                res('error','用户名已存在');
                return false;
            }else{
                $data['username']=$request->post('username');
                $data['password']=md5($request->post('password'));
                $data['regtime']=time();
                if(md5($request->post('password'))!=md5($request->post('password'))){
                    res('error','两次密码不一致');
                }
                $reg=Db::name('user')->insertGetId($data);
                if($reg){
                    session('userid',$reg);
                    res('success','注册成功');
                }else{
                    res('error','失败，请重试');
                }    
            }
        }else{
            jsBack('请求有误');
        }
    }
    /**
    * 登陆
    */ 
    public function login(Request $request)
    {
        $where['username']=$request->post('username');
        $where['password']=md5($request->post('password'));
        //用户是否存在
        $user=Db::name('user')->where(['username'=>$request->post('username')])->find();
        if(!$user){
            jsBack('用户不存在');
            return false;
        }else{
            // 写入session
            $userid=Db::name('user')->where($where)->value('userid');
            if($userid){
                session('userid',$userid);
                jsBack('登陆成功',"/api/index");
            }else{
                jsBack('密码有误');
            }    
        }
    }
    /**
    * 个人资料完善
    */
    public function gerenadd()
    {
        if(session('userid')==''){
            jaBack('请前去登陆','/api/register');
            return false;
        }
        $where['userid']=session('userid');
        $user=Db::name('user')->where($where)->find();
        $this->assign('user',$user);
        $this->assign('title','资料修改');
        return view('gerenadd');
    }
    public function gerenInsert()
    {   if(!request()->isPost()){
            $this->redirect('index/gerenadd');
            return false;
        }
        $file=request()->file('photo');
        if($file!=''){
           $info=$file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/photo');
            $data['photo']='/public/uploads/photo/'.date("Ymd").'/'.$info->getFilename(); 
        }else{
        
        }
        $data['nickname']=input('post.nickname');
        $data['birthday']=strtotime(input('post.birthday'));
        $data['userinter']=input('post.userinter');
        $where['userid']=session('userid');
        $user=Db::name('user')->where($where)->update($data);
        if($user){
            jsBack('修改成功','/api/index/geren');
        }else{
            jsBack('修改失败');
        }
    } 
    /**
    * 密码找回
    */
    public function pwdBack()
    {
        echo "密码找回";
    } 
    //退出登录
    public function logout(){
        session(null);
        jsBack("退出成功",'/api/index');
    }
}